import 'package:ati_package/core/utils/ati_color.dart';
import 'package:ati_package/pages/common/ati_scaffold.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/route_manager.dart';
import 'package:qr_flutter/qr_flutter.dart';

class UserCodeView extends StatefulWidget {
  const UserCodeView({super.key});

  @override
  State<UserCodeView> createState() => _UserCodeViewState();
}

class _UserCodeViewState extends State<UserCodeView> {
  @override
  bool _saving = false;

  Widget build(BuildContext context) {
    return AtiScaffold(
      title: '我的二维码',

      body: Column(children: [_buildCodeWidget(), _buildFootBtn()]),
    );
  }

  _buildCodeWidget() {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 25.r, horizontal: 30.r),
      padding: EdgeInsets.symmetric(vertical: 20.r, horizontal: 35.r),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10.r),
      ),
      child: Column(
        children: [
          Row(
            children: [
              CircleAvatar(
                radius: 30.r,
                backgroundColor: const Color(0xFFC23A8E),
                child: Text(
                  '浪子'.substring(0, 1),
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 22.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
              SizedBox(width: 12.5.r),
              Text(
                '浪子',
                style: TextStyle(
                  fontSize: 16.sp,
                  color: Colors.black87,
                  fontWeight: FontWeight.w600,
                ),
              ),
              SizedBox(width: 2.5.r),
              Icon(Icons.person, color: AtiColor.primary, size: 17.5.r),
            ],
          ),
          SizedBox(height: 15.r),

          QrImageView(
            data: 'https://example.com/user/12345',
            version: QrVersions.auto,
            size: Get.width - 130.r,
            backgroundColor: Colors.white,
            eyeStyle: const QrEyeStyle(
              eyeShape: QrEyeShape.square, // 👈 改成方形，视觉更规整
              color: Colors.black,
            ),
            dataModuleStyle: const QrDataModuleStyle(
              dataModuleShape: QrDataModuleShape.square, // 👈 与眼形保持一致
              color: Colors.black,
            ),
          ),
          SizedBox(height: 5.r),
          Text(
            '扫一扫加我为好友',
            style: TextStyle(fontSize: 14.sp, color: Colors.grey),
          ),
        ],
      ),
    );
  }

  _buildFootBtn() {
    return GestureDetector(
      onTap: _saving
          ? null
          : () {
              setState(() => _saving = true);
            },
      child: AnimatedContainer(
        margin: EdgeInsets.symmetric(vertical: 12.5.r, horizontal: 30.r),

        duration: const Duration(milliseconds: 300),
        width: double.infinity,
        height: 45.h,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(30.r),
          gradient: LinearGradient(
            colors: _saving
                ? [const Color(0xFF22C55E), const Color(0xFF10B981)]
                : [const Color(0xFF22C55E), const Color(0xFF10B981)],
          ),
        ),
        child: Center(
          child: _saving
              ? SizedBox(
                  width: 22.5.r,
                  height: 22.5.r,
                  child: const CircularProgressIndicator(color: Colors.white),
                )
              : Text(
                  '保存到相册',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w600,
                  ),
                ),
        ),
      ),
    );
  }
}
